#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;
const int MAXN = 110;
//是否使用过了
bool vis[MAXN], ori[MAXN];
vector<int> G[MAXN];
//每个方案
int pos;
vector<int> fangan[MAXN];
int n, m, k;
//产物 原料
int pro[MAXN];
string fs[MAXN];

//按照顺序排序
bool cmp(int a, int b) {
    for (int i = 0;; i++)
        if (fangan[a][i] != fangan[b][i])
            return fangan[a] < fangan[b];
}

bool dfs(int lev, vector<string> &path) {
    if (lev == m) {
        for (int i = 0; i < path.size(); i++) {
            if (i)
                cout << endl;
            cout << path[i];
        }
        return true;
    }

    for (int i = 0; i < G[pro[lev]].size(); i++) {
        bool op = true;
        for (int j = 0; j < fangan[G[pro[lev]][i]].size(); j++)
            if (vis[fangan[G[pro[lev]][i]][j]]) {
                op = false;
                break;
            }
        //方案不合法
        if (!op)
            continue;
        for (int j = 0; j < fangan[G[pro[lev]][i]].size(); j++)
            vis[fangan[G[pro[lev]][i]][j]] = true;
        //回溯
        path.push_back(fs[G[pro[lev]][i]]);
        if (dfs(lev + 1, path))
            return true;
        for (int j = 0; j < fangan[G[pro[lev]][i]].size(); j++)
            vis[fangan[G[pro[lev]][i]][j]] = false;
        path.pop_back();
    }
    return false;
}


int main() {
    string s;
    int a;

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a;
        ori[a] = true;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
        cin >> pro[i];
        //自身可以成为方案
        if (ori[pro[i]]) {
            string ss;
            if (pro[i] < 10)
                ss = "0" + to_string(pro[i]) + " -> " + "0" + to_string(pro[i]);
            else
                ss = to_string(pro[i]) + " -> " + to_string(pro[i]);
            fs[pos] = ss;
            G[pro[i]].push_back(pos);
            fangan[pos++] = vector<int>({pro[i]});
        }
    }
    cin >> k;
    getchar();
    while (k--) {
        vector<int> tmp;
        getline(cin, s);
        for (int i = 0; i < s.length(); i = i + 5) {
        	//空格代表这是化学式右边的产物，自己看看规律
            if (s[i] == ' ') {
                //产物
                a = stoi(s.substr(i + 1, 2));
                //映射字符串
                fs[pos] = s;
                G[a].push_back(pos);
                //映射数字
                fangan[pos++] = tmp;
                break;
            }
            int cur = stoi(s.substr(i, 2));
            //原料没有这个元素
            if (!ori[cur])
                break;
            tmp.push_back(cur);
        }
    }
	//根据要求的顺序排序
    for (int i = 0; i < m; i++)
        sort(G[pro[i]].begin(), G[pro[i]].end(), cmp);

    vector<string> path;
    //dfs求解
    dfs(0, path);

    return 0;
}
